<template>
  <!-- 顾客管理 -->
  <div class="user">
    <!-- 头部区域 -->
    <div class="header">
      <!-- 添加按钮 -->
      <el-button type="primary" @click="addUser">添加</el-button>
      <!-- 顾客用户名输入框 -->
      <el-input
        style="width: 200px"
        placeholder="请输入用户名"
        v-model="userName"
        clearable
      >
      </el-input>
      <!-- 顾客状态下拉框 -->
      <el-select v-model="userStatus" clearable placeholder="请选择状态">
        <el-option label="正常" value="正常"> </el-option>
        <el-option label="禁用" value="禁用"> </el-option>
      </el-select>
      <!-- 查询按钮 -->
      <el-button type="primary" @click="queryUser">查询</el-button>
    </div>
    <!-- 表格内部展示区 -->
    <div class="table_content">
      <!-- :data整个表格的数据源 -->
      <el-table :data="userData" style="width: 100%" size="mini">
        <!-- 编号 -->
        <el-table-column
          prop="id"
          label="编号"
          align="center"
          fixed="left"
          width="100px"
        >
        </el-table-column>
        <!-- 用户名 -->
        <el-table-column
          prop="username"
          label="用户名"
          align="center"

        >
        </el-table-column>
        <!-- 姓名 -->
        <el-table-column
          prop="realname"
          label="姓名"
          align="center"

        >
        </el-table-column>
        <!-- 手机号 -->
        <el-table-column
          prop="telephone"
          label="手机号"
          align="center"

        >
        </el-table-column>
        <!-- 性别 -->
        <el-table-column
          prop="gender"
          label="性别"
          align="center"

        >
          <template slot-scope="scope">
            <i
              class="el-icon-male green_info"
              :src="scope.row.gender"
              v-if="scope.row.gender == 'male'"
            ></i>
            <i
              class="el-icon-female red_info"
              :src="scope.row.gender"
              v-if="scope.row.gender == 'female'"
            ></i>
            <i :src="scope.row.gender" v-if="scope.row.gender == null">暂无</i>
          </template>
        </el-table-column>
        <!-- 状态 -->
        <el-table-column
          prop="status"
          label="状态"
          align="center"

        >
          <template slot-scope="scope">
            <span
              class="el-tag el-tag--success el-tag--mini el-tag--light"
              :src="scope.row.status"
              v-if="scope.row.status == '正常'"
              >{{ scope.row.status }}</span
            >
            <span
              class="el-tag el-tag--danger el-tag--mini el-tag--light"
              :src="scope.row.status"
              v-else
              >{{ scope.row.status }}</span
            >
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" align="center" width="150px">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              style="margin-right: 2px"
              @click="editUser(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              style="color: #e65d6e; margin-left: 2px"
              @click="deleteUser(scope.row.id)"
              >删除</el-button
            >
            <el-button
              type="text"
              size="small"
              style="color: blue; margin-left: 2px"
              @click="toMe(scope.row.id)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 底部分页区域 -->
    <div class="paginetion">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 模态框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisable"
      width="50%"
      center
      @close="dialogClose"
    >
      <!-- 表单区域 -->
      <!-- :model表单对象 最终我们要提交的那个表单对象 -->
      <!-- :rules 表单验证的规则 -->
      <!-- ref类比与id最终通过ref找到这张表单 -->
      <el-form
        :model="userForm"
        :rules="rules"
        ref="user_form"
        label-width="80px"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input
            placeholder="请输入用户名"
            v-model="userForm.username"
            clearable
          ></el-input>
        </el-form-item>
        <!-- 姓名 -->
        <el-form-item label="姓名" prop="realname">
          <el-input
            placeholder="请输入姓名"
            v-model="userForm.realname"
            clearable
          ></el-input>
        </el-form-item>
        <!-- 手机号 -->
        <el-form-item label="手机号" prop="telephone">
          <el-input
            placeholder="请输入手机号"
            v-model.number="userForm.telephone"
            clearable
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="userForm.password"
            show-password
            clearable
          ></el-input>
        </el-form-item>
        <!-- 性别 -->
        <el-form-item label="性别" prop="gender">
          <el-radio v-model="userForm.gender" label="male">男</el-radio>
          <el-radio v-model="userForm.gender" label="female">女</el-radio>
        </el-form-item>
        <!-- 状态 -->
        <el-form-item label="状态" prop="status" v-show="visible">
          <el-radio v-model="userForm.status" label="正常">正常</el-radio>
          <el-radio v-model="userForm.status" label="禁用">禁用</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <el-button
          type="primary"
          @click="addForm"
          v-if="dialogTitle == '添加顾客'"
          >确 定</el-button
        >
        <el-button
          type="primary"
          @click="editForm"
          v-if="dialogTitle == '编辑顾客信息'"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
//引入axios
import { get, post } from "@/utils/request";
export default {
  data() {
    return {
      //初始化顾客状态
      userStatus: "",
      //初始化顾客用户名
      userName: "",
      //顾客参数数组
      userData: [],
      //当前页数
      currentPage: 1,
      //每页显示条数
      pageSize: 8,
      //权限id设置为顾客
      roleId: 6,
      //页面展示数据的总条数
      total: 10,
      //定义一个查询参数
      queryData: [],
      userForm: {
        username: "",
        realname: "",
        telephone: "",
        password: "",
        gender: "0",
        status: "正常",
      },
      dialogTitle: "",
      dialogVisable: false,
      visible: false,
      //表单验证规则
      rules: {
        //用户名
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        //姓名
        realname: [
          { required: true, message: "请输入姓名", trigger: "change" },
        ],
        //电话
        telephone: [
          { required: true, message: "请输入电话号码", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "danger" },
        ],
        //性别
        gender: [{ required: true, message: "请选择性别", trigger: "change" }],
      },
    };
  },
  computed: {},
  methods: {
    async getAllUserData() {
      //获取参数
      let data = {
        page: this.currentPage,
        pageSize: this.pageSize,
        roleId: this.roleId,
      };
      let res = await get("/baseUser/pageQuery", {
        ...data,
        ...this.queryData,
      });
      //将data中的顾客数组 进行赋值
      this.userData = res.data.list;
      //页面中数据条数 赋值
      this.total = res.data.total;
    },
    // description控制每页的条数
    // params val
    handleSizeChange(val) {
      //将每页要展示的条数进行更新
      this.pageSize = val;
      //将当前的页数 重置到第一页
      this.currentPage = 1;
      //重新调用获取顾客信息的方法
      this.getAllUserData();
    },
    handleCurrentChange(val) {
      //当前页数 与 分页插件动态点击控制的页数相绑定
      this.currentPage = val;
      //重新调用获取顾客信息的方法
      this.getAllUserData();
    },
    //查询顾客
    queryUser() {
      let data = {
        //顾客用户名
        username: this.userName,
        //顾客状态
        status: this.userStatus,
      };
      console.log(data);
      //格式化参数 当某个属性没有值 将其删除 以防网络请求的操作
      for (const key in data) {
        if (!data[key]) {
          delete data[key];
        }
      }
      //将参数赋值给queryData
      this.queryData = data;
      //将页数强制加载回第一页
      this.currentPage = 1;
      //重新获取产品数据
      this.getAllUserData();
    },
    //添加产品操作
    addUser() {
      //清空表单对象
      this.userForm = {};
      //设置状态隐藏
      this.visible = false;
      //设置模态框标题
      this.dialogTitle = "添加顾客";
      //模态框显示出来
      this.dialogVisable = true;
    },
    // 编辑产品操作
    editUser(row) {
      //将当前点击的这一行的对象 赋值到 对象里
      //浅拷贝 赋值对象的引用 对象的内存地址 用的还有事同一个
      //深拷贝
      this.userForm = { ...row };
      //设置状态可显示
      this.visible = true;
      //设置模态框标题
      this.dialogTitle = "编辑顾客信息";
      //模态框显示出来
      this.dialogVisable = true;
    },
    //关闭模态框 调用表单重置的方法
    dialogClose() {
      //重置表单
      this.$refs["user_form"].resetFields();
      //关闭模态框
      setTimeout(() => {
        this.dialogVisable = false;
      }, 200);
    },
    //表单提交 添加用户信息
    addForm() {
      this.$refs["user_form"].validate(async (valid) => {
        if (valid) {
          //发送表单验证请求 提交表单对象
          let res = await post("/user/register", this.userForm);
          //给用户一个成功的提示
          if (res.status == 200) {
            this.$message({
              message: res.message,
              type: "success",
            });
          } else {
            this.$message.error(res.message);
          }
          //刷新页面
          this.getAllUserData();
          //调用关闭模态框的方法
          this.dialogVisable = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    //表单提交 修改用户信息
    editForm() {
      this.$refs["user_form"].validate(async (valid) => {
        if (valid) {
          //发送表单验证请求 提交表单对象
          let res = await post("/baseUser/saveOrUpdate", this.userForm);
          //给用户一个成功的提示
          if (res.status == 200) {
            this.$message({
              message: res.message,
              type: "success",
            });
          } else {
            this.$message.error(res.message);
          }
          //刷新页面
          this.getAllUserData();
          //调用关闭模态框的方法
          this.dialogVisable = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 删除用户
    async deleteUser(id) {
      let res = await get("/baseUser/deleteById", {
        id,
      });
      if (res.status == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
      } else {
        this.$message.error(res.message);
      }
      //刷新页面
        this.getAllUserData();

    },
    // 详情跳转
    toMe(id) {
      this.$router.push({
        path: "/admin/custome/Details",
        query:{ id }
      });
    },
  },
  created() {
    //自动获取顾客数据
    this.getAllUserData();
  },
  mounted() {},
};
</script>
<style scoped>
html {
  line-height: 1.15;
  font-size: 12px;
}
.paginetion {
  position: fixed;
  bottom: 30px;
  right: 10px;
}
</style>